<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
        }

        .slide {
            width: 300px;
            /* height: 100px; */
            border: 3px solid #ccc;
            box-sizing: border-box;
            /* overflow: hidden; */
        }

        .slide-con {
            display: flex;
        }

        .slide-item {
            min-width: 100%;
            list-style: none;
            float: left;
        }

        .slide-item img {
            width: 100%;
            height: 100%;
        }

        .toggleAnimation {
            transition: all .5s linear;
        }
    </style>
</head>

<body>
    <div class="slide">
        <ul class="slide-con">
            <li class="slide-item">
                <img src="./imgs/4.jpg" alt="">
            </li>
            <li class="slide-item">
                <img src="./imgs/1.jpeg" alt="">
            </li>
            <li class="slide-item">
                <img src="./imgs/3.jpg" alt="">
            </li>
            <li class="slide-item">
                <img src="./imgs/4.jpg" alt="">
            </li>
            <li class="slide-item">
                <img src="./imgs/1.jpeg" alt="">
            </li>
        </ul>
    </div>
    <div>
        <button class="prev">上一张</button>
        <button class="next">下一张</button>
    </div>
</body>
<script>
    const prevEl = document.querySelector(".prev");
    const nextEl = document.querySelector(".next");
    const slideCon = document.querySelector(".slide-con");
    const slideItem = document.querySelector(".slide-item").getBoundingClientRect();
    let index = 0;
    slideCon.style.transform = `translateX(${-slideItem.width}px)`

    nextEl.addEventListener('click', () => {
        index--;
        console.log("#######", index);

        if (index == -4) {
            index = 0
            slideCon.classList.remove('toggleAnimation')
            slideCon.style.transform = `translateX(${-index*slideItem.width}px)`
            setTimeout(() => {
                nextEl.click();
            }, 0)
            return;
        }
        console.log("#####!!!!##", index * slideItem.width);

        slideCon.classList.add('toggleAnimation')
        slideCon.style.transform = `translateX(${index*slideItem.width}px)`
    })
    prevEl.addEventListener('click', () => {
        index++;
        console.log("#######", index);

        if (index == 1) {
            console.log("#######");
            index = -3
            slideCon.classList.remove('toggleAnimation')
            slideCon.style.transform = `translateX(${-index*slideItem.width}px)`
            setTimeout(() => {
                prevEl.click()
            }, 0)
            return;
        }
        slideCon.classList.add('toggleAnimation')
        slideCon.style.transform = `translateX(${index*slideItem.width}px)`
    })

</script>

</html>